<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="4"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="jzyOption"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <el-button v-show="dealerType !== '25381002'" size="small" type="primary" @click="addHandle">新增</el-button>
        <!-- <el-button size="small">下载</el-button>
        <el-button size="small">导入</el-button> -->
      </template>
    </FilterTable>
    <BaseDialog
      :visible.sync="visible"
      destroyOnClose
      width="30%"
      :title="title"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <OptionModal v-if="visible" :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getBrandList, querySeriesDict ,getModelData,getSeriesList,getOptions,getPackageListByModel} from '@/api/common/cars/brandcarsModels';
import {getPackageList} from '@/api/common/cars/masterDataCars';
import OptionModal from './OptionModal';

import Cookies from 'js-cookie';
export default {
  name: 'Option',
  mixins: [dictionary],
  components: {
    OptionModal
  },
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: getOptions,
      fetchParams: {},
      visible: false,
      handleType: '',
      formData: {},
      title:"",
      dealerType: Cookies.get('dealerType'),
    };
  },
  created() {
    this.getDictData();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '品牌名称',
          fieldName: 'brandId',
          placeholder: '请选择...',
          itemList: [],
          change: val => {
              this.changeBrandSelect(val)
          }
        },
        {
          type: 'SELECT',
          label: '车系名称',
          fieldName: 'seriesId',
          placeholder: '请选择...',
          itemList: [],
          change: val => {
              this.changeBrandModelId(val)
          }
        },
        {
          type: 'SELECT',
          label: '车型名称',
          fieldName: 'modelId',
          placeholder: '请选择...',
          itemList: [],
          change: val => {
              this.changeBrandPackageId(val)
          }
        },
        {
          type: 'SELECT',
          label: '配置名称',
          fieldName: 'packageId',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '选装名称',
          fieldName: 'optionName',
          placeholder: '配置名称...'
        },
        {
          type: 'INPUT',
          label: '选装代码',
          fieldName: 'optionCode',
          placeholder: '配置代码...'
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSources',
          placeholder: '请选择...',
          itemList: this.createDictList('1045')
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择...',
          itemList: this.createDictList('1004')
        }
      ];
    },
    createTableColumns() {
      let dealerType = Cookies.get('dealerType');
      let dataType = Cookies.get('dataType');
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 60,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button containerStyle={dealerType !== '25381002' ? {}:{display:'none'}} size="mini" type="text" disabled={dataType === '10461001' && (props.row.dataSources == 10451002 || props.row.dataSources == 10451003)} onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '品牌名称',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'configName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装代码',
          dataIndex: 'optionCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'dataSources',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1045'),
          render: props => {
            return <span>{this.createDictText(props.row.dataSources, '1045')}</span>;
          }
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1004')}</span>;
          }
        }
      ];
    },
    async getDictData() {
      const res = await getBrandList();
      if (res.resultCode === 200) {
        this.topFilterList.find(item=>item.fieldName==='brandId').itemList = res.data.map(x => ({ value: x.brandId, text: x.brandName }));
      }
    },
    // 车系
    async changeBrandSelect(val){
        if (!val) {
          this.topFilterList.find(item=>item.fieldName==='seriesId').initialValue = '';
          this.topFilterList.find(item=>item.fieldName==='seriesId').itemList = [];
          this.topFilterList.find(item=>item.fieldName==='modelId').initialValue = '';
          this.topFilterList.find(item=>item.fieldName==='modelId').itemList = [];
          this.topFilterList.find(item=>item.fieldName==='packageId').initialValue = '';
          this.topFilterList.find(item=>item.fieldName==='packageId').itemList = [];
        } else {
          const res = await getSeriesList(val);
          if (res.resultCode === 200) {
            this.topFilterList.find(item=>item.fieldName==='modelId').initialValue = '';
            this.topFilterList.find(item=>item.fieldName==='modelId').itemList = [];
            this.topFilterList.find(item=>item.fieldName==='seriesId').initialValue = '';
            this.topFilterList.find(item=>item.fieldName==='seriesId').itemList = res.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
          }
        }
    },
    // 车型
    async changeBrandModelId(val){
      let brandId = this.topFilterList.find(item=>item.fieldName==='brandId').initialValue;
        if (!val) {
          this.topFilterList.find(item=>item.fieldName==='modelId').initialValue = '';
          this.topFilterList.find(item=>item.fieldName==='modelId').itemList = [];
          this.topFilterList.find(item=>item.fieldName==='packageId').initialValue = '';
          this.topFilterList.find(item=>item.fieldName==='packageId').itemList = [];
        } else {
          const res = await getModelData(brandId,val);
          if (res.resultCode === 200) {
            this.topFilterList.find(item=>item.fieldName==='modelId').initialValue = '';
            this.topFilterList.find(item=>item.fieldName==='modelId').itemList = res.data.map(x => ({ value: x.modelId, text: x.modelName }));
          }
        }
    },
    // 配置下拉框
    async changeBrandPackageId(modelId){
      const brandsid = this.topFilterList.find(item=>item.fieldName==='brandId').initialValue;
      const seriessid = this.topFilterList.find(item=>item.fieldName==='seriesId').initialValue;
        this.topFilterList.find(item=>item.fieldName==='packageId').initialValue = '';
        this.topFilterList.find(item=>item.fieldName==='packageId').itemList = [];
      if (modelId) {

        const res = await getPackageListByModel(brandsid, seriessid, modelId);
        if (res.resultCode === 200) {
          this.topFilterList.find(item=>item.fieldName==='packageId').itemList = res.data.map(x => ({ value: x.packageId, text: x.configName }));
        }
      }
    },

    addHandle() {
      this.handleType = 'add';
      this.visible = true;
      this.title="新增"
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.visible = true;
      this.title="编辑"
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
};
</script>

<style lang="less" scoped>
</style>
